1 module hunt.security.acl.User; 2 3 import hunt.security.acl.Role; 4 import hunt.security.acl.permission.Permission; 5 6 class User 7 { 8 private 9 { 10 int _id; 11 string _name; 12 Permission _permission; 13 Role[] roles; 14 __gshared User _default; 15 } 16 17 public this(int id , string name) 18 { 19 this._id = id; 20 this._permission = new Permission; 21 this._name = name; 22 } 23 24 static public User defaultUser(){ 25 if(_default is null) 26 _default = new User(0 , "default"); 27 return _default; 28 } 29 30 31 public int id() 32 { 33 return this._id; 34 } 35 36 public string name() 37 { 38 return this._name; 39 } 40 41 public bool isGuest() 42 { 43 return this._id == 0; 44 } 45 46 public bool hasRole(int roleId) 47 { 48 foreach(r ; roles) 49 if(r.id == roleId) 50 return true; 51 52 return false; 53 } 54 55 public bool can(string key) 56 { 57 return this._permission.hasPermission(key); 58 } 59 60 public User assignRole(Role role) 61 { 62 this.roles ~= role; 63 64 this._permission.addPermissions(role.permission.permissions); 65 66 return this; 67 } 68 }